﻿using com.guojin.mj.net.message.game;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class CurrentWarRecordPage : EventManager
{
    private static CurrentWarRecordPage instance;

    public static CurrentWarRecordPage GetInstance()
    {
        return instance;
    }

    void Awake()
    {
        instance = this;
    }

    public override void InformationSetting()
    {
        AddListenerToButton(closeBtn, CloseBtnClick);
        MessageManager.GetInstance().SendMsg1241(GameData.GetInstance().userID);
    }

    public Text[] timeArr;
    public Text zhuangGold;
    public GameObject playerGroup;
    public GameObject headGroup;
    public Button closeBtn;

    public void Init(CurrentWarRecordRet cpr)
    {
        SetGameTimeToPage(cpr.gameTime);

        TooL.DestroyAllChildren(playerGroup);
        TooL.DestroyAllChildren(headGroup);

        if (cpr.list != null)
        {
            var results  = from item in cpr.list orderby item.settleScore descending select item;

            foreach (var item in results)
            {
                var temp = TooL.LoadPrefab(playerGroup, "currenWarRecordItem");
                if (item.userId.ToString() == GameData.GetInstance().userID)
                {
                    SetTextSelect(temp.transform.GetChild(0).GetComponent<Text>());
                    SetTextSelect(temp.transform.GetChild(1).GetComponent<Text>());
                    SetTextSelect(temp.transform.GetChild(2).GetComponent<Text>());
                }
                SetText(temp.transform.GetChild(0).GetComponent<Text>(), item.name);
                SetText(temp.transform.GetChild(1).GetComponent<Text>(), item.takeInScore.ToString());
                SetText(temp.transform.GetChild(2).GetComponent<Text>(), item.settleScore.ToString());
            }
        }


        if (cpr.gameUser != null)
        {
            for (int i = 0; i < cpr.gameUser.Count; i++)
            {
                var temp = TooL.LoadPrefab(headGroup, "CurrentWarRecordHead");
                SetText(temp.transform.FindChild("name").GetComponent<Text>(), cpr.gameUser[i].userName);
                DownloadHeadTexture(temp.transform.FindChild("head/mask/headImg").gameObject.GetComponent<RawImage>(),
                    cpr.gameUser[i].avatar);
            }
        }
 

        if (GameRoomModel.GetInstance().RoomType == 0)
        {
            if (GameData.GetInstance().userID == GameData.GetInstance().roomHostUserID)
            {
                if (!string.IsNullOrEmpty(cpr.zhuangGold.ToString()))
                {
                    SetText(zhuangGold, "总抢庄金币：" + cpr.zhuangGold.ToString());
                }
            }
        }
        else if (GameRoomModel.GetInstance().RoomType == 1)
        {
            if (GameData.GetInstance().clubDic[GameRoomModel.GetInstance().ClubOrLianMengName] > 0)
            {
                if (!string.IsNullOrEmpty(cpr.zhuangGold.ToString()))
                {
                    SetText(zhuangGold, "总抢庄金币：" + cpr.zhuangGold.ToString());
                }
            }
        }
        else if (GameRoomModel.GetInstance().RoomType == 2)
        {
            string[] str = GameRoomModel.GetInstance().ClubOrLianMengName.Split(',');
            
            if (GameData.GetInstance().lianMengDic[str[0]] > 0)
            {
                if (!string.IsNullOrEmpty(cpr.zhuangGold.ToString()))
                {
                    SetText(zhuangGold, "总抢庄金币：" + cpr.zhuangGold.ToString());
                }
            }

            for (int i = 1; i < str.Length; i++)
            {
                foreach (var item in GameData.GetInstance().clubDic)
                {
                    if (item.Key == str[i] && item.Value > 0)
                    {
                        if (!string.IsNullOrEmpty(cpr.zhuangGold.ToString()))
                        {
                            SetText(zhuangGold, "总抢庄金币：" + cpr.zhuangGold.ToString());
                        }
                    }
                }
            }
        }
    }


    private int hour = 0;
    private int min = 0;
    private int second = 0;
    private void SetGameTimeToPage(string gameTime)
    {
        Debug.Log(gameTime);
        int time = int.Parse( gameTime );
        hour = time / 3600;
        min =(time %3600 ) / 60;
        second = (int)time % 60;

        SetGameTime();
    }

    private void SetGameTime()
    {
        if (hour < 10)
        {
            timeArr[0].text ="0"+ hour + ":";
        }
        else
        {
            timeArr[0].text = hour + ":";
        }

        if (min < 10)
        {
            timeArr[1].text = "0"+min + ":";
        }
        else
        {
            timeArr[1].text =  min + ":";
        }

        if (second < 10)
        {
            timeArr[2].text = "0"+second.ToString();
        }
        else
        {
            timeArr[2].text = second.ToString();
        }  
    }

    private float timer;
    void Update()
    {
        if (timer <= 0)
        {
            timer = second;
        }

        timer -= Time.deltaTime;

        if (second != Math.Floor(timer))
        {
            if (Math.Floor(timer) < 0)
            {
                if (min == 0)
                {
                    if (hour > 0)
                    {
                        hour -= 1;
                        min = 59;
                        second = 59;
                        SetGameTime();
                    }
                    else
                    {
                        hour = 0;
                        min = 0;
                        second = 0;
                        SetGameTime();
                    }
                }
                else
                {
                    min -= 1;
                    second = 59;
                    SetGameTime();
                }
            }
            else
            {
                second = (int)timer;
                SetGameTime();
            }
        }
    }
}
